package yxk.list.odc.homs.personwaiton;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.entity.datamodel.ListSelectedRow;
import kd.bos.entity.datamodel.ListSelectedRowCollection;
import kd.bos.form.control.Button;
import kd.bos.form.control.events.BeforeItemClickEvent;
import kd.bos.form.events.AfterDoOperationEventArgs;
import kd.bos.list.IListView;
import kd.bos.list.plugin.AbstractListPlugin;
import kd.bos.logging.Log;
import kd.bos.logging.LogFactory;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import yxk.list.odc.homs.util.OnboardUtil;

import java.util.*;

/*
 *
 *
 *@author lqh
 *@date 2023/9/21
 *
 */
public class PersonWaitonListPlugin extends AbstractListPlugin {

    private static final Log LOG = LogFactory.getLog(PersonWaitonListPlugin.class);
    private OnboardUtil onboardUtil=new OnboardUtil();
    @Override
    public void registerListener(EventObject e) {
        super.registerListener(e);
        addItemClickListeners("btn_confirmonbrd");
    }

    @Override
    public void beforeItemClick(BeforeItemClickEvent evt) {
        super.beforeItemClick(evt);
        String itemKey = evt.getItemKey();
        if ("btn_confirmonbrd".equals(itemKey)) {
            ListSelectedRowCollection selectedRows = ((IListView) this.getView()).getSelectedRows();
            Iterator<ListSelectedRow> rows = selectedRows.iterator();
            while (rows.hasNext()) {
                ListSelectedRow row = rows.next();
                Object primaryKeyValue = row.getPrimaryKeyValue();
                String billNo = row.getBillNo();
                DynamicObject dynamicObject = BusinessDataServiceHelper.loadSingle("hom_onbrdinfo", new QFilter[]{new QFilter("id", QCP.equals, primaryKeyValue)});
                Map<String, Object> params = new HashMap<>();
                String candidatenumber = dynamicObject.getString("candidatenumber");//候选人编码
                Integer probationtime = dynamicObject.getInt("probationtime");//试用期
                String offerNumber = dynamicObject.getString("offernumber");//offer编号
                Date effectdate = dynamicObject.getDate("effectdate");
                String token = onboardUtil.getZhaoPinToken();
                Map<String, Object> result = onboardUtil.getHired(token, candidatenumber, probationtime, effectdate);
                //Map<String, Object> result = OnboardUtil.getHired(token, candidatenumber, probationtime);
                Boolean isSuccess = (Boolean) result.get("success");
                if (!isSuccess) {
                    String errorMessage = (String) result.get("errorMessage");
                    this.getView().showMessage(errorMessage);
                    //evt.setCancel(true);
                } else {
                    this.getView().showMessage("入职成功");
                }
                LOG.info("result:", result);
            }
        }
    }

    @Override
    public void afterDoOperation(AfterDoOperationEventArgs args) {
        super.afterDoOperation(args);
        String operateKey = args.getOperateKey();
        if ("doconfirm".equals(operateKey)) {
            ListSelectedRowCollection selectedRows = ((IListView) this.getView()).getSelectedRows();
            Iterator<ListSelectedRow> rows = selectedRows.iterator();
            while (rows.hasNext()) {
                ListSelectedRow row = rows.next();
                Object primaryKeyValue = row.getPrimaryKeyValue();
                String billNo = row.getBillNo();
                DynamicObject dynamicObject = BusinessDataServiceHelper.loadSingle("hom_onbrdinfo", new QFilter[]{new QFilter("id", QCP.equals, primaryKeyValue)});
                Map<String, Object> params = new HashMap<>();
                String candidatenumber = dynamicObject.getString("candidatenumber");//候选人编码
                Integer probationtime = dynamicObject.getInt("probationtime");//试用期
                String offerNumber = dynamicObject.getString("offernumber");//offer编号
                Date effectdate = dynamicObject.getDate("effectdate");
                String token = onboardUtil.getZhaoPinToken();
                Map<String, Object> result = onboardUtil.getHired(token, candidatenumber, probationtime, effectdate);
                //Map<String, Object> result = OnboardUtil.getHired(token, candidatenumber, probationtime);
                Boolean isSuccess = (Boolean) result.get("success");
                if (!isSuccess) {
                    String errorMessage = (String) result.get("errorMessage");
                    this.getView().showMessage(errorMessage);
                    //evt.setCancel(true);
                } else {
                    this.getView().showMessage("入职成功");
                }
                LOG.info("result:", result);
            }
        }

    }
}
